package com.xyzwps.server.paimon.model.service

import com.xyzwps.server.paimon.model.entity.User
import com.xyzwps.server.paimon.model.entity.UserSession
import com.xyzwps.server.paimon.model.repository.UserSessionRepository
import org.springframework.stereotype.Service
import com.xyzwps.server.paimon.common.Utils.genUid
import com.xyzwps.server.paimon.ext.instantNow
import com.xyzwps.server.paimon.ext.plusDays

@Service
class UserSessionDomainService(
    private val userSessionRepository: UserSessionRepository
) {

    fun createSession(user: User): UserSession {
        val token = genUid()
        val session = UserSession(
            id = null, token,
            userId = user.id!!,
            createdAt = instantNow(),
            expireTime = instantNow().plusDays(7)
        )
        userSessionRepository.save(session)
        return session
    }

    fun getSession(token: String): UserSession? = userSessionRepository.findByToken(token)
}
